"""
Case Type   : GUC
Case Name   : 验证gs_guc set方式设置参数enable_partition_opfusion为on，设置成功
Create At   : 2024/10/14
Owner       : nanyang12
Description :
        1.查询enable_partition_opfusion默认值=off
        2.使用gs_guc set方式设置参数enable_partition_opfusion为on，并查看修改后的参数值
        3.执行简单DML操作
        4.清理环境，恢复参数默认值
Expect      :
        1.默认值"enable_opfusion=off"
        2.执行成功，修改后的参数值为on
        3.执行成功
        4.默认值恢复成功
History     :
"""
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()
commonsh = CommonSH('dbuser')


class DeveloperOptions(unittest.TestCase):
    def setUp(self):
        self.constant = Constant()
        self.user_node = Node('dbuser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        LOG.info(
            '------Opengauss_Function_DeveloperOptions_Case0122start----')

    def test_post_auth_delay(self):
        LOG.info('--步骤1:查询enable_partition_opfusion默认值为off--')
        sql_cmd = commonsh.execut_db_sql('''show enable_partition_opfusion;''')
        LOG.info(sql_cmd)
        self.assertIn(self.constant.BOOLEAN_VALUES[1], sql_cmd)
        LOG.info('--步骤2:使用gs_guc set方式设置参数enable_partition_opfusion为on;expect:成功--')
        msg = commonsh.execute_gsguc('set',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     'enable_partition_opfusion=on')
        LOG.info(msg)
        self.assertTrue(msg)
        LOG.info('--步骤2.2:查看参数enable_partition_opfusion修改后的值为on--')
        sql_cmd = commonsh.execut_db_sql('show enable_partition_opfusion;')
        LOG.info(sql_cmd)
        self.assertIn(self.constant.BOOLEAN_VALUES[0], sql_cmd)

        LOG.info('--步骤3:执行简单DML操作--')
        sql_cmd = '''drop table if exists t_opfusion_0122 cascade;
                     create table t_opfusion_0122(v1 int, v2 char(20));
                     insert into t_opfusion_0122 values(generate_series(0,100000), 'enable_opfusion');
                     update t_opfusion_0122 set v2 = 'test' where v1 < 20000;
                     select count(*) from t_opfusion_0122;
                     delete from t_opfusion_0122;
                     drop table if exists t_opfusion_0122 cascade;
                    '''
        excute_cmd1 = f'''source {self.DB_ENV_PATH};
            gsql -d postgres -p {self.user_node.db_port} -c "{sql_cmd}"
            '''
        LOG.info(excute_cmd1)
        msg1 = self.user_node.sh(excute_cmd1).result()
        LOG.info(msg1)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, msg1)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg1)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg1)
        self.assertIn(self.constant.UPDATE_SUCCESS_MSG, msg1)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, msg1)
        self.assertIn('100001', msg1)

    def tearDown(self):
        LOG.info('--步骤4:恢复默认值--')
        msg = commonsh.execute_gsguc('set',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     'enable_partition_opfusion=off')
        LOG.info(msg)
        msg = commonsh.restart_db_cluster()
        LOG.info(msg)
        status = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status in status)
        LOG.info(
            '------Opengauss_Function_DeveloperOptions_Case0122finish----')
